﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace WpfPdfReader
{
    /// <summary>
    /// Interaction logic for RecentItemControl.xaml
    /// </summary>
    public partial class RecentItemControl : UserControl
    {
        private FileItemViewModelCollection FileItemItemCollection = null;

        public RecentItemControl()
        {
            InitializeComponent();
            this.DataContext = FileItemItemCollection = new FileItemViewModelCollection();
            this.listView.ItemsSource = FileItemItemCollection.ItemCollection;
        }

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listView.SelectedIndex == -1)
                return;
            FileItemViewModel model = this.listView.SelectedItem as FileItemViewModel;
            if (ItemSelectionChanged != null)
            {
                this.ItemSelectionChanged(sender, new FileItemSelectionChangedEventArgs(model));
            }
            this.listView.SelectedIndex = -1;
        }

        public event EventHandler<FileItemSelectionChangedEventArgs> ItemSelectionChanged;
        public event RoutedEventHandler Open;
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="fileName"></param>
        public void AddFileItem(string fileName, Bitmap titlePageBmp)
        {
            this.FileItemItemCollection.Add(fileName, 0, titlePageBmp, null);
        }

        public void AddFileItem(string fileName, int page, Bitmap lastPageBmp)
        {
            this.FileItemItemCollection.Add(fileName, page, null, lastPageBmp);
        }

        public void AddBookMark(string fileName, string bookMarkName, int page, Bitmap pageBmp,string note)
        {
            this.FileItemItemCollection.AddBookMark(fileName, bookMarkName, page, pageBmp,note);
        }

        public FileItemViewModel FindItem(string filName)
        {
            return this.FileItemItemCollection.FindItem(filName);
        }

        public List<BookMark> GetBookMarks(string fileName)
        {
            return this.FileItemItemCollection.GetBookMarks(fileName);
        }

        /// <summary>
        /// 将最近打开的文件保存文xml文件
        /// </summary>
        public void Serialize()
        {
            this.FileItemItemCollection.Serialize();
        }

        /// <summary>
        /// 是否有最近打开的文件
        /// </summary>
        public bool HasItem
        {
            get { return this.FileItemItemCollection.ItemCollection.Count > 0; }
        }

        public class FileItemSelectionChangedEventArgs : EventArgs
        {
            private FileItemViewModel item = null;
            public FileItemViewModel Item { get { return this.item; } }
            public FileItemSelectionChangedEventArgs(FileItemViewModel item)
            {
                this.item = item;
            }
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.Open != null)
                this.Open(sender, e);
        }
    }
}
